package io.renren.modules.store.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.FastIDUtil;
import io.renren.modules.store.entity.TStoreDiningTableEntity;
import io.renren.modules.store.entity.TStoreTypeEntity;
import io.renren.modules.store.service.TStoreDiningTableService;
import io.renren.modules.store.service.TStoreTypeService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.store.entity.TStoreRegionEntity;
import io.renren.modules.store.service.TStoreRegionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-24 01:14:45
 */
@RestController
@RequestMapping("store/tstoreregion")
public class TStoreRegionController extends AbstractController {
    @Autowired
    private TStoreRegionService tStoreRegionService;
    @Autowired
    private TStoreTypeService storeTypeService;
    @Autowired
    private TStoreDiningTableService diningTableService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoreregion:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("status",params.get("status"));
        params.put("storeTypeId",params.get("storeTypeId"));
        PageUtils page = tStoreRegionService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getRegionList")
    @RequiresPermissions("store:tstoreregion:list")
    public R getRegionList(@RequestBody TStoreRegionEntity regionEntity){
        List<List<TStoreRegionEntity>> list = new ArrayList<>();
        regionEntity.setSellerId(getSeller());
        for (Long l : regionEntity.getStoreTypeIdList()){
            regionEntity.setStoreTypeId(l);
            List<TStoreRegionEntity> regionEntityList = tStoreRegionService.getRegionList(regionEntity);
            list.add(regionEntityList);
        }
        return R.ok().put("regionList",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstoreregion:info")
    public R info(@PathVariable("id") Long id){
		TStoreRegionEntity tStoreRegion = tStoreRegionService.getById(id);

        return R.ok().put("tStoreRegion", tStoreRegion);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoreregion:save")
    public R save(@RequestBody TStoreRegionEntity tStoreRegion){
        TStoreRegionEntity one = tStoreRegionService.getOne(new QueryWrapper<TStoreRegionEntity>()
                .eq("store_type_id", tStoreRegion.getStoreTypeId())
                .eq("seller_id", getSeller())
                .eq("region_name", tStoreRegion.getRegionName())
        );
        if (one != null){
            throw new RRException("该餐厅类型下已存在同名区域,请重新输入");
        }
        tStoreRegion.setRegionNo(FastIDUtil.fastId());
        tStoreRegion.setSellerId(getSeller());
        tStoreRegion.setStatus(1);
		tStoreRegionService.save(tStoreRegion);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoreregion:update")
    public R update(@RequestBody TStoreRegionEntity tStoreRegion){
        TStoreRegionEntity one = tStoreRegionService.getOne(new QueryWrapper<TStoreRegionEntity>()
                .eq("store_type_id", tStoreRegion.getStoreTypeId())
                .eq("seller_id", getSeller())
                .eq("region_name", tStoreRegion.getRegionName())
                .notIn("id",tStoreRegion.getId())
        );
        if (one != null){
            throw new RRException("该餐厅类型下已存在同名区域,请重新修改");
        }
		tStoreRegionService.updateById(tStoreRegion);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoreregion:delete")
    public R delete(@RequestBody Long[] ids){
        TStoreDiningTableEntity tableEntity = diningTableService.getOne(new QueryWrapper<TStoreDiningTableEntity>()
            .eq("seller_id",getSeller())
                .eq("region_id",ids[0])
        );
        if (tableEntity != null){
            throw new RRException("此区域下分配了餐台,不能删除");
        }
        tStoreRegionService.removeById(ids[0]);

        return R.ok();
    }

}
